package com.smart.system.service;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.smart.mybatis.service.impl.BaseServiceImpl;
import com.smart.system.dao.JwznBusinessDataDao;
import com.smart.entity.system.JwznBusinessDataEntity;
import com.smart.service.system.JwznBusinessDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.Calendar;

/**
 * final ServiceImpl
 *
 * @author cgz
 * @since 2025-04-30 11:30:33
 */
@Service("jwznBusinessDataService")
@Transactional(rollbackFor = Exception.class)
public class JwznBusinessDataServiceImpl extends BaseServiceImpl<JwznBusinessDataDao, JwznBusinessDataEntity> implements JwznBusinessDataService {

    @Autowired
    private JwznBusinessDataDao jwznBusinessDataMapper;
    @Override
    public List<Map<String, Integer>> getNameCountMap() {
        return jwznBusinessDataMapper.groupCountByName();
    }

    @Override
    public List<Map<String, Integer>> getNameConsumeMap(DateTime startTime, DateTime endTime) {
        return jwznBusinessDataMapper.groupCountByNameAndPickTime(startTime,endTime);
    }


//    /**
//     *
//     * 该方法用于查询特定名称在指定日期内的总耗值
//     *
//     * @author treasure
//     * @date 2025/5/7 16:23
//     * @param name 客户名称，用于筛选消费数据
//     * @param start 开始时间
//     * @param end 结束时间
//     * @return 返回耗电量
//     */
//    @Override
//    public Double getConsumptionByNameAndTimeRange(String name, Date start, Date end) {
//        QueryWrapper<JwznBusinessDataEntity> wrapper = new QueryWrapper<>();
//        wrapper.eq("name", name)
//               .ge("pick_time", start)
//               .lt("pick_time", end);
//
//        List<JwznBusinessDataEntity> dataList = jwznBusinessDataMapper.selectList(wrapper);
//
//        if (dataList.isEmpty()) {
//            return 0.0;
//        }
//
//        double totalConsumption = 0.0;
//        for (JwznBusinessDataEntity data : dataList) {
//            totalConsumption += data.getConsume();
//        }
//
//        return totalConsumption;
//


    @Override
    public List<Map<String, Object>> getDailyLineChartData(String name, Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        List<Map<String, Object>> result = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            Date start = calendar.getTime();
            Calendar startCalendar = Calendar.getInstance();
            startCalendar.setTime(start);
            calendar.add(Calendar.HOUR_OF_DAY, 2);
            Date end = calendar.getTime();
            Calendar endCalendar = Calendar.getInstance();
            endCalendar.setTime(end);

            QueryWrapper<JwznBusinessDataEntity> wrapper = new QueryWrapper<>();
            if (name != null) {
                wrapper.eq("name", name);
            }
            wrapper.ge("pick_time", start)
                   .lt("pick_time", end);

            List<JwznBusinessDataEntity> dataList = jwznBusinessDataMapper.selectList(wrapper);

            double totalConsumption = 0.0;
            for (JwznBusinessDataEntity data : dataList) {
                totalConsumption += data.getConsume();
            }

            Map<String, Object> item = new HashMap<>();
            item.put("time", String.format("%02d:00 - %02d:00", startCalendar.get(Calendar.HOUR_OF_DAY), endCalendar.get(Calendar.HOUR_OF_DAY)));
            item.put("consumption", totalConsumption);
            result.add(item);
        }

        return result;
    }

    @Override
    public List<Map<String, Object>> getWeeklyLineChartData(String name, Date startDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

        List<Map<String, Object>> result = new ArrayList<>();
        Date today = new Date();

        for (int i = 0; i < 7; i++) {
            Date start = calendar.getTime();
            calendar.add(Calendar.DAY_OF_YEAR, 1);
            Date end = calendar.getTime();

            if (end.after(today)) {
                break;
            }

            QueryWrapper<JwznBusinessDataEntity> wrapper = new QueryWrapper<>();
            if (name != null) {
                wrapper.eq("name", name);
            }
            wrapper.ge("pick_time", start)
                   .lt("pick_time", end);

            List<JwznBusinessDataEntity> dataList = jwznBusinessDataMapper.selectList(wrapper);

            double totalConsumption = 0.0;
            for (JwznBusinessDataEntity data : dataList) {
                totalConsumption += data.getConsume();
            }

            Map<String, Object> item = new HashMap<>();
            item.put("time", start);
            item.put("consumption", totalConsumption);
            result.add(item);
        }

        return result;
    }

    @Override
    public List<Map<String, Object>> getMonthlyLineChartData(String name, Date startDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        calendar.set(Calendar.DAY_OF_MONTH, 1);

        List<Map<String, Object>> result = new ArrayList<>();
        Date today = new Date();

        while (true) {
            Date start = calendar.getTime();
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            Date end = calendar.getTime();

            if (end.after(today)) {
                break;
            }

            QueryWrapper<JwznBusinessDataEntity> wrapper = new QueryWrapper<>();
            if (name != null) {
                wrapper.eq("name", name);
            }
            wrapper.ge("pick_time", start)
                   .lt("pick_time", end);

            List<JwznBusinessDataEntity> dataList = jwznBusinessDataMapper.selectList(wrapper);

            double totalConsumption = 0.0;
            for (JwznBusinessDataEntity data : dataList) {
                totalConsumption += data.getConsume();
            }

            Map<String, Object> item = new HashMap<>();
            item.put("time", start);
            item.put("consumption", totalConsumption);
            result.add(item);
        }

        return result;
    }
}